package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import org.example.bean.Book;
import org.example.bean.ShoppingCart;
import org.example.config.JwtConfig;
import org.example.mapper.ShoppingCartMapper;
import org.example.service.BookService;
import org.example.service.ShoppingCartService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private BookService bookService;

    @Override
    public JsonResult add(String bookId, String token) {
        Claims claims = JwtConfig.parseJWT(token);
        String userId = claims.get("id").toString();
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        //判断是否在购物车中存在
        Boolean hasKey = ops.hasKey("gwc"+userId, bookId);
        if (hasKey) {
            //存在
            ops.increment("gwc"+userId,bookId,1);
        }else {
            //不存在
            ops.put("gwc"+userId, bookId, "1");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult find(String token) {
        Claims claims = JwtConfig.parseJWT(token);
        String userId = claims.get("id").toString();
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        //当前用户的购物车
        Map<Object, Object> cart = ops.entries("gwc"+userId);
        List<ShoppingCart> list = new ArrayList<>();
        //循环遍历cart的keys
        cart.keySet().forEach(bookId -> {
            int number = Integer.parseInt(cart.get(bookId).toString());
            ShoppingCart sc = new ShoppingCart();
            sc.setId(bookId.toString());
            sc.setNumber(number);
            Book book = bookService.getById(bookId.toString());
            sc.setPublisher(book.getPublisher());
            sc.setBookName(book.getName());
            sc.setPrice(book.getPrice());
            sc.setImageName(book.getImageName());
            list.add(sc);
        });
        return ResultTool.success(list);
    }

    @Override
    public JsonResult changCartNumber(String token, String bookId, int number) {
        Claims claims = JwtConfig.parseJWT(token);
        String useId = claims.get("id").toString();
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        ops.increment("gwc"+useId, bookId, number);
        return ResultTool.success("success");
    }

    @Override
    public JsonResult changCartNumber2(String token, String bookId, int number) {
        Claims claims = JwtConfig.parseJWT(token);
        String useId = claims.get("id").toString();
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        ops.put("gwc"+useId, bookId, number+"");
        return ResultTool.success("success");
    }

    @Override
    public JsonResult removeCartById(String token, String bookId) {
        Claims claims = JwtConfig.parseJWT(token);
        String useId = claims.get("id").toString();
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        ops.delete("gwc"+useId, bookId);
        return ResultTool.success("success");
    }

    @Override
    public JsonResult batchRemoveCart(String token, String[] ids) {
        Claims claims = JwtConfig.parseJWT(token);
        String useId = claims.get("id").toString();
        HashOperations<String, Object, Object> ops = stringRedisTemplate.opsForHash();
        for (int i =0;i<ids.length;i++){
            ops.delete("gwc"+useId, ids[i]);
        }
        return ResultTool.success("success");
    }
}
